package com.java110.mall.cart.bmo.storeCart.impl;

import com.java110.dto.distributionMode.DistributionModeDto;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.storeOrder.StoreCartDto;
import com.java110.mall.cart.bmo.storeCart.IGetStoreCartBMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.intf.cart.IStoreCartInnerServiceSMO;
import com.java110.intf.goods.IDistributionModeInnerServiceSMO;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getStoreCartBMOImpl")
public class GetStoreCartBMOImpl implements IGetStoreCartBMO {

    @Autowired
    private IStoreCartInnerServiceSMO storeCartInnerServiceSMOImpl;

    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IDistributionModeInnerServiceSMO distributionModeInnerServiceSMOImpl;

    /**
     * @param storeCartDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(StoreCartDto storeCartDto) {


        int count = storeCartInnerServiceSMOImpl.queryStoreCartsCount(storeCartDto);

        List<StoreCartDto> storeCartDtos = null;
        if (count > 0) {
            storeCartDtos = storeCartInnerServiceSMOImpl.queryStoreCarts(storeCartDto);
            freshStoreCartDtos(storeCartDtos);
        } else {
            storeCartDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) storeCartDto.getRow()), count, storeCartDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    private void freshStoreCartDtos(List<StoreCartDto> storeCartDtos) {

        if (storeCartDtos == null || storeCartDtos.size() < 1) {
            return;
        }

        //输入
        List<String> productIds = new ArrayList<>();
        List<String> modeIds = new ArrayList<>();

        for (StoreCartDto tmpProductDto : storeCartDtos) {
            productIds.add(tmpProductDto.getProductId());
            modeIds.add(tmpProductDto.getModeId());

        }

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjIds(productIds.toArray(new String[productIds.size()]));
        fileRelDto.setRelTypeCd(FileRelDto.REL_TYPE_CD_GOODS_COVER);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);

//        // 商城派送费用
//        DistributionModeDto distributionModeDto = new DistributionModeDto();
//        distributionModeDto.setModeIds(modeIds.toArray(new String[modeIds.size()]));
//        distributionModeDto.setShopId(storeCartDtos.get(0).getShopId());
//        List<DistributionModeDto> distributionModeDtos = distributionModeInnerServiceSMOImpl.queryDistributionModes(distributionModeDto);
//        List<DistributionModeDto> tmpDistributionModeDtos = null;

        //刷入图片信息
        List<String> photoVos = null;
        String url = null;
        String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN, "IMG_PATH");
        imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
        for (StoreCartDto tmpProductDto : storeCartDtos) {
            for (FileRelDto tmpFileRelDto : fileRelDtos) {
                if (!tmpProductDto.getProductId().equals(tmpFileRelDto.getObjId())) {
                    //url = "/callComponent/download/getFile/file?fileId=" + tmpFileRelDto.getFileRealName() + "&communityId=-1";
                    continue;
                }
                if (StringUtil.isEmpty(tmpFileRelDto.getFileRealName())) {
                    continue;
                }
                url = imgUrl + tmpFileRelDto.getFileRealName();
                if (tmpFileRelDto.getFileRealName().startsWith("http")) {
                    url = tmpFileRelDto.getFileRealName();
                }

                tmpProductDto.setCoverPhoto(url);
            }

//            tmpDistributionModeDtos = new ArrayList<>();
//            for (DistributionModeDto tmpDistributionModeDto : distributionModeDtos) {
//                if (tmpProductDto.getModeId().equals(tmpDistributionModeDto.getModeId())) {
//                    tmpDistributionModeDtos.add(tmpDistributionModeDto);
//                }
//            }
//            tmpProductDto.setDistributionModeDtos(tmpDistributionModeDtos);
            tmpProductDto.setFeeModeType(MappingCache.getValue(DistributionModeDto.DOMAIN_AMLL, DistributionModeDto.FEE_MODE_TYPE));

        }


    }

}
